package weaver.jobs.cese;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.cese.util.CommUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-08-17
 * @Description: 定时任务更新五大类采购计划的状态和剩余招标周期
 * @ModifyLog:
 **/

public class WeaPurchasePlanCronJob extends BaseCronJob {
    @Override
    public void execute() {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        rs.executeQuery( "select n.zcdbjcrq,n.id,n.xqrq,m.dbjcsprq,m.requestid,m.zgshwcrq,m.zbwjfbrq,m.tbjzrq,m.slkbrq,m.pbjghqrq,m.zbtzrq \n" +
                "from uf_cgxq_wl n left join\n" +
                "(select a.id,a.xqdid,b.dbjcsprq,b.requestid,b.zgshwcrq,b.zbwjfbrq,b.tbjzrq,b.slkbrq,b.pbjghqrq,b.zbtzrq \n" +
                "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                "on n.id=m.xqdid where n.zcdbjcrq is not null or m.dbjcsprq is not null" );
        while (rs.next()) {
            String zcdbjcrq = Util.null2String( rs.getString( "zcdbjcrq" ) );//最迟定标决策日期
            String id = Util.null2String( rs.getString( "id" ) );
            String xqrq = Util.null2String( rs.getString( "xqrq" ) );//需求创建日期
            String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );//定标决策审批日期
            String requestid = Util.null2String( rs.getString( "requestid" ) );
            String zgshwcrq = Util.null2String( rs.getString( "zgshwcrq" ) );//资格审核完成日期
            String zbwjfbrq = Util.null2String( rs.getString( "zbwjfbrq" ) );//招标文件发布日期
            String tbjzrq = Util.null2String( rs.getString( "tbjzrq" ) );//投标截止日期
            String slkbrq = Util.null2String( rs.getString( "slkbrq" ) );//首轮开标日期
            String pbjghqrq = Util.null2String( rs.getString( "pbjghqrq" ) );//评标结果会签日期
            String zbtzrq = Util.null2String( rs.getString( "zbtzrq" ) );//定标决策审批日期

            if ("".equals( dbjcsprq )) {
                long syzbzq = CommUtil.getDays( zcdbjcrq );
                long pcrq = CommUtil.getDays( xqrq ) + 2;
                if (syzbzq <= 0) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=0 ,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq <= 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=0 ,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                } else if (syzbzq > 7) {
                    rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=0 ,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                }
            } else {
                long syzbzq = CommUtil.getDays( dbjcsprq );
                //招标采购立项
                rs1.executeQuery( "select count(n.requestid) as count1 from formtable_main_1033 n\n" +
                        "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.requestid =?", requestid );
                rs1.next();
                int count1 = Util.getIntValue( rs1.getString( "count1" ) );
                if (count1 > 0) {
                    long pcrq = CommUtil.getDays( xqrq ) + 4;
                    if (syzbzq <= 0) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq <= 7) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq > 7) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    }
                }
                //供应商资格预审
                rs1.executeQuery( "select count(n.requestid) as count2 from formtable_main_1038 n\n" +
                        "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =? ", requestid );
                rs1.next();
                int count2 = Util.getIntValue( rs1.getString( "count2" ) );
                if (count2 > 0) {
                    long pcrq = CommUtil.getDays( zgshwcrq );
                    if (syzbzq <= 0) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq <= 7) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq > 7) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    }
                }
                //发起招标
                rs1.executeQuery( "select count(n.requestid) as count3 from formtable_main_1042 n\n" +
                        "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
                rs1.next();
                int count3 = Util.getIntValue( rs1.getString( "count3" ) );
                if (count3 > 0) {
                    long pcrq = CommUtil.getDays( zbwjfbrq );
                    if (syzbzq <= 0) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq <= 7) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq > 7) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    }
                }
                //供应商投标
                rs1.executeQuery( "select count(n.requestid) as count4 from formtable_main_1043 n\n" +
                        "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
                rs1.next();
                int count4 = Util.getIntValue( rs1.getString( "count4" ) );
                if (count4 > 0) {
                    long pcrq = CommUtil.getDays( tbjzrq );
                    if (syzbzq <= 0) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq <= 5) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq > 5) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    }
                }
                //开标结果会签
                rs1.executeQuery( "select count(n.requestid) as count5 from formtable_main_1046 n\n" +
                        "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
                rs1.next();
                int count5 = Util.getIntValue( rs1.getString( "count5" ) );
                if (count5 > 0) {
                    long pcrq = CommUtil.getDays( slkbrq );
                    if (syzbzq <= 0) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq <= 5) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq > 5) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    }
                }
                //评标结果会签
                rs1.executeQuery( "select count(n.requestid) as count6 from formtable_main_1049 n\n" +
                        "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
                rs1.next();
                int count6 = Util.getIntValue( rs1.getString( "count6" ) );
                if (count6 > 0) {
                    long pcrq = CommUtil.getDays( pbjghqrq );
                    if (syzbzq <= 0) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=6,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq <= 2) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=6,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq > 2) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=6,t.pcsj=? where id=?", syzbzq, pcrq, id );
                    }
                }
                //供应商发起决策
                rs1.executeQuery( "select count(n.requestid) as count7 from formtable_main_1050 n\n" +
                        "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
                rs1.next();
                int count7 = Util.getIntValue( rs1.getString( "count7" ) );
                if (count7 > 0) {
                    long pcrq = CommUtil.getDays( dbjcsprq );
                    if (syzbzq <= 0) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq <= 2) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq > 2) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    }
                }
                //中标通知
                rs1.executeQuery( "select count(n.requestid) as count8 from formtable_main_1052 n\n" +
                        "left join workflow_requestbase m on n.requestid=m.requestid where m.currentnodetype !=3 and n.cglxd =?", requestid );
                rs1.next();
                int count8 = Util.getIntValue( rs1.getString( "count8" ) );
                if (count8 > 0) {
                    long pcrq = CommUtil.getDays( zbtzrq );
                    if (syzbzq <= 0) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq <= 7) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    } else if (syzbzq > 7) {
                        rs1.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                    }
                }
            }
        }
    }
}
